// Layout组件
import Layout from '@/components/Layout.vue';

// 路由信息
let route_tree = (state, getters) => {
    // 筛选一级路由
    let route_1st = state.routes.filter((route) => route.parent_id === 0);

    // 树形结构递归函数
    let parseToTree = (origin, list) => {
        // 使用map生成新数组
        return list.map((parent) => {
            // 结构数据
            let { route_path, route_name, require_auth, route_title, component_path } = parent;
            // 尝试筛选子级路由
            let children = origin.filter((route) => route.parent_id === parent.route_id);

            // 基础路由对象
            let route = {
                path: route_path,
                name: route_name,
                component: '',
                meta: { require_auth, route_title, },
            };

            // 判断是否拥有子级路由
            if (children.length > 0) {
                route.component = Layout;
                route.children = parseToTree(origin, children);
            } else {
                route.component = () => import(`@/views${component_path}`);
            }
            return route;
        });

    }
    return parseToTree(state.routes, route_1st);
}

// 查询操作按钮权限
let has_operation = (state, getters) => {
    return (code) => {
        // includes只适合于简单数组
        return state.operations.some((operation) => operation.permission_code === code);
    }
}

export default {
    route_tree,
    has_operation,
}